﻿using MtApi;
using MTEA.LIB;
using MTEA.LIB.Analysis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTEA.MTTask
{
    public class MTTask<T> :IMTTask where T : MTTaskConfig
    {
        protected T config;
        protected bool isFinished;
        protected MtApiClient client;
        public MTTask(T config)
        {
            this.config = config;
        }
        public bool IsFinished
        {
            get
            {
                return isFinished;
            }

        }

        public int Id { get; set; }

        public string  Name { get; protected set; }

        public string Description { get; protected set; }
        public virtual void Execute(MTTaskContext context)
        {
            client = context.Client;
            Id = int.MinValue;
        }

        protected bool OrderTrade(string tag, MTTaskContext context)
        {
            if (config.OrderType == OrderType.Close)
            {
                Debug.Assert(config.TickId != -1);
                if (context.CloseOrder(config.TickId))
                {
                    MTLogcat.i(tag, "CloseOrder Successfuly");
                    isFinished = true;
                }
                else
                {
                    MTLogcat.w(tag, "CloseOrder Failed");
                    isFinished = false;
                }
                return isFinished;
            }
            else if (config.OrderType == OrderType.Buy)
            {
                if (context.BuyOrder(config.Lots))
                {
                    MTLogcat.i(tag, "BuyOrder Successfuly");
                    isFinished = true;
                }
                else
                {
                    MTLogcat.w(tag, "BuyOrder Failed");
                    isFinished = false;
                }
                return isFinished;
            }
            else if (config.OrderType == OrderType.Sell)
            {
                if (context.SellOrder(config.Lots))
                {
                    MTLogcat.i(tag, "SellOrder Successfuly");
                    isFinished = true;
                }
                else
                {
                    MTLogcat.w(tag, "SellOrder Failed");
                    isFinished = false;
                }
                return isFinished;
            }
            else
            {
                throw new ArgumentException("Invalid OrderType");
            }
        }
    }

   
}
